package com.jerry.project;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.CompositeFilterOperator;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.appengine.labs.repackaged.org.json.JSONArray;
import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.appengine.labs.repackaged.org.json.JSONObject;

public class CommentServlet extends HttpServlet {
	@Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws IOException {
		JSONObject jsonobj=null;
		String commentstring=null;
		String id=null;
		String user=null;
		
		try {
			jsonobj=new JSONObject(reqToJSONString.reqToJSONString(req));
			if(jsonobj.has("id")){
				id=jsonobj.getString("id");
			}
			if(jsonobj.has("comment")){
				commentstring=jsonobj.getString("comment");
			}
			if(jsonobj.has("user")){
				user=jsonobj.getString("user");
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
        Key commentKey = KeyFactory.createKey("CommentKind", "comment");
        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        //use id to identify Event
        Date createTime = new Date();
        if(commentstring!=null&&id!=null){
        	Entity comment = new Entity("Comment", commentKey);
        	comment.setProperty("id", id);
        	comment.setProperty("comment", commentstring);
        	comment.setProperty("user", user);
        	comment.setProperty("createTime", createTime);
        	datastore.put(comment);
        	resp.getWriter().write(1);
        }
        
        resp.getWriter().write(0);
	}
}